package jiang;

import java.util.Random;

public class VolatileTest4 implements Runnable {
	volatile VolatileTest4_R resource;

	public VolatileTest4(VolatileTest4_R i) {
		resource = i;
	}

	public static void main(String[] args) {
		VolatileTest4 vt = new VolatileTest4(new VolatileTest4_R(0, 0));
		new Thread(vt).start();
		new Thread(vt).start();
		while (Thread.activeCount() > 1) {
			Thread.yield();
		}
	}

	@Override
	public void run() {
		resource.fun();
		resource = new VolatileTest4_R(new Random().nextInt(100) + 1,
				new Random().nextInt(100) + 1);
	}
}

class VolatileTest4_R {
	int i = 0;
	int j = 0;

	public VolatileTest4_R(int i, int j) {
		this.i = i;
		this.j = j;
	}

	@Override
	public String toString() {
		return "VolatileTest4_R [i=" + i + ", j=" + j + "]";
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	public int getJ() {
		return j;
	}

	public void setJ(int j) {
		this.j = j;
	}

	public void fun() {
		int a = i;
		int b = j;
		System.out.println("fun() -> " + a + " | " + b);
	}

}